package cn.bon.emp.controller;

import cn.bon.common.bean.JsonResult;
import cn.bon.common.tool.R;
import cn.bon.emp.api.pojo.po.Org;
import cn.bon.emp.api.pojo.vo.OrgVO;
import cn.bon.emp.service.IOrgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Lc_cl
 * @Date 2023/12/9
 * @Description
 */

@Tag(name = "组织机构相关接口")
@RestController
@RequestMapping("/org")
public class OrgController {
    @Resource
    IOrgService orgService;

    @Operation(summary = "查询所有组织机构")
    @GetMapping("/all")
    public JsonResult<List<OrgVO>> getAll() {
        return R.success(orgService.getAll());
    }

    @Operation(summary = "添加子节点")
    @PostMapping("/{fatherId}")
    public JsonResult<OrgVO> addOrg(@PathVariable("fatherId") @Schema(description = "父节点Id") Long fatherId,
                                     @RequestParam @Schema(description = "新增节点名称") String name,
                                     @RequestParam @Schema(description = "新增节点排序") Integer order) {
        Org org = new Org().setParentId(fatherId).setOrgName(name).setOrgOrder(order);
        OrgVO vo = orgService.add(org);
        return R.success(vo);
    }


    @Operation(summary = "编辑节点")
    @PutMapping("/{id}")
    public JsonResult<OrgVO> editOrg(@PathVariable("id") @Schema(description = "节点Id") Long id,
                                      @RequestParam @Schema(description = "修改名称") String name,
                                      @RequestParam @Schema(description = "修改排序") Integer order) {
        //填入org
        Org org = new Org().setOrgId(id).setOrgName(name).setOrgOrder(order);
        OrgVO edited = orgService.edit(org);
        return R.success(edited);
    }


    @Operation(summary = "删除节点")
    @DeleteMapping("/{id}")
    public JsonResult<String> del(@PathVariable("id") @Schema(description = "删除节点ID") Long id) {
        orgService.delete(id);
        return R.success("删除成功");
    }
}
